import 'package:flutter/material.dart';
import 'package:hbtrans_flutter/main/first/firstpage.dart';
import 'package:hbtrans_flutter/main/message/message.dart';

import 'mine.dart';
import 'service.dart';

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage>
    with SingleTickerProviderStateMixin {
  var titles = ['首页', '服务', '资讯', '我的'];
  int _currentIndex = 0;

  String title = '客户';
  List<BottomNavigationBarItem> icons = new List();

  var _controller = PageController(
    initialPage: 0,
  );

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  List<BottomNavigationBarItem> setIcons() {
    icons.clear();
    double iconSize = 20;

    icons.add(BottomNavigationBarItem(
        icon: _currentIndex == 0
            ? Image.asset(
                'images/ic_home_s.png',
                height: iconSize,
                width: iconSize,
                fit: BoxFit.fill,
              )
            : Image.asset(
                'images/ic_home_un.png',
                height: iconSize,
                width: iconSize,
                fit: BoxFit.fill,
              ),
        title: Text(
          titles[0],
        )));
    icons.add(BottomNavigationBarItem(
        icon: _currentIndex == 1
            ? Image.asset(
                'images/ic_trans_s.png',
                height: iconSize,
                width: iconSize,
                fit: BoxFit.fill,
              )
            : Image.asset(
                'images/ic_trans_un.png',
                height: iconSize,
                width: iconSize,
                fit: BoxFit.fill,
              ),
        title: Text(
          titles[1],
        )));
    icons.add(BottomNavigationBarItem(
        icon: _currentIndex == 2
            ? Image.asset(
                'images/ic_news_s.png',
                height: iconSize,
                width: iconSize,
                fit: BoxFit.fill,
              )
            : Image.asset(
                'images/ic_news_un.png',
                height: iconSize,
                width: iconSize,
                fit: BoxFit.fill,
              ),
        title: Text(
          titles[2],
        )));
    icons.add(BottomNavigationBarItem(
        icon: _currentIndex == 3
            ? Image.asset(
                'images/ic_usercenter_s.png',
                height: iconSize,
                width: iconSize,
                fit: BoxFit.fill,
              )
            : Image.asset(
                'images/ic_usercenter_un.png',
                height: iconSize,
                width: iconSize,
                fit: BoxFit.fill,
              ),
        title: Text(
          titles[3],
        )));

    return icons;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _controller,
        children: [FirstPage(), Service(), Message(), Mine()],
        physics: NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          selectedItemColor: const Color(0xff3e3a39),
          unselectedItemColor: const Color(0xffC3C3C3),
          selectedFontSize: 10,
          unselectedFontSize: 10,
          onTap: (index) {
            title = titles[index];
            _controller.jumpToPage(index);
            setState(() {
              _currentIndex = index;
            });
          },
          type: BottomNavigationBarType.fixed,
          items: setIcons()),
    );
  }
}
